<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <el-form ref="form" :rules="rules" label-width="120px" size="mini" :model="formData">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="formData.name" style="width: 80%;" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="formData.code" style="width: 80%;" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item prop="managerId" label="部门负责人">
        <el-select v-model="formData.managerId" style="width: 80%;" placeholder="请选择负责人">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />

        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="formData.introduce" type="textarea" style="width: 80%;" size="mini" placeholder="1-100个字符" :rows="4" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="addDepts">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getDeptList, getManager, addDepts, getDeptDetail, updateDeptDetail } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      formData: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      rules: {
        code: [{
          required: true,
          message: '不能为空',
          trigger: 'blur'
        }, {
          min: 2,
          max: 10,
          message: '2-10个字符',
          trigger: 'blur'
        }, {
          validator: async(rule, value, callback) => {
            let res = await getDeptList()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }

            if (res.some(item => item.code === value)) {
              callback(new Error('此部门编码已存在'))
            } else {
              callback()
            }
          }
        }],
        introduce: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 100,
            message: '1-100个字符',
            trigger: 'blur'
          }
        ],
        managerId: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 10,
            message: '2-10个字符',
            trigger: 'blur'
          },
          {
            validator: async(rule, value, callback) => {
              let res = await getDeptList()
              if (this.formData.id) {
                res = res.filter(item => item.id !== this.formData.id)
              }

              if (res.some(item => item.name === value)) {
                callback(new Error('此部门名称已存在'))
              } else {
                callback()
              }
            }
          }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManager()
  },
  methods: {
    close() {
      // 手动重置表单
      this.formData = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getManager() {
      this.managerList = await getManager()
    },
    async addDepts() {
      this.$refs.form.validate(async isOk => {
        if (isOk) {
          let msg = '新增'
          if (this.formData.id) {
            // 编辑功能
            msg = '编辑'
            await updateDeptDetail(this.formData)
          } else {
            // 扩展运算符，并对pid进行赋值
            // 新增功能
            await addDepts({ ...this.formData, pid: this.currentNodeId })
          }
          this.$message.success(`${msg}部门成功`)
          // 通知父组件同步更新
          this.$emit('success')
          this.close()
        }
      })
    },
    async addDeptDetail() {
      this.formData = await getDeptDetail(this.currentNodeId)
    }
  }
}
</script>
